package com.ansion.basedb.storage.secure;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import androidx.annotation.NonNull;


import com.ansion.basedb.storage.api.SpStorage;
import com.ansion.utils.ResourceUtils;
import com.ansion.utils.hash.Base64Helper;

import java.util.Map;

public class SpStorageImpl implements SpStorage {
    private SharedPreferences kvs = null;
    private static final String prefix = "normal_sp_";
    SharedPreferences.Editor editor;

    public SpStorageImpl(@NonNull String name) {
        kvs = ResourceUtils.getContext().getSharedPreferences(getName(name), Context.MODE_PRIVATE);
        editor = kvs.edit();
    }

    public static String getName(@NonNull String name) {
        return prefix + name;
    }

    @Override
    public boolean remove(@NonNull String key) {
        editor.remove(key);
        return editor.commit();
    }

    @Override
    public boolean contains(@NonNull String key) {
        return kvs.contains(key);
    }

    @Override
    public boolean putInt(@NonNull String key, int value) {
        editor.putInt(key, value);
        return editor.commit();
    }

    @Override
    public int getInt(@NonNull String key, int defValue) {
        return kvs.getInt(key, defValue);
    }

    @Override
    public boolean putLong(@NonNull String key, long value) {
        editor.putLong(key, value);
        return editor.commit();
    }

    @Override
    public long getLong(@NonNull String key, long defValue) {
        return kvs.getLong(key, defValue);
    }

    @Override
    public boolean putFloat(@NonNull String key, float value) {
        editor.putFloat(key, value);
        return editor.commit();
    }

    @Override
    public float getFloat(@NonNull String key, float defValue) {
        return kvs.getFloat(key, defValue);
    }

    @Override
    public boolean putString(@NonNull String key, String value) {
        editor.putString(key, value);
        return editor.commit();
    }

    @Override
    public String getString(@NonNull String key, String defValue) {
        return kvs.getString(key, defValue);
    }

    @Override
    public boolean putBoolean(@NonNull String key, boolean value) {
        editor.putBoolean(key, value);
        return editor.commit();
    }

    @Override
    public boolean getBoolean(@NonNull String key, boolean defValue) {
        return kvs.getBoolean(key, defValue);
    }

    @Override
    public boolean putByteArray(@NonNull String key, byte[] value) {

        editor.putString(key, Base64Helper.encode(value));
        return editor.commit();
    }

    @Override
    public byte[] getByteArray(@NonNull String key, byte[] defValue) {
        String value = kvs.getString(key, "");
        if (TextUtils.isEmpty(value)) {
            return defValue;
        }
        return Base64Helper.decode(value);
    }

    @Override
    public void clearAll() {
        editor.clear().commit();
    }


    @Override
    public SharedPreferences.Editor getEditor() {
        return editor;
    }

    @Override
    public Map<String, ?> getAll() {
        return kvs.getAll();
    }
}
